<template>
  <div class="announcement-panel">
    <h2>系统公告</h2>
    <div class="announcement-content">
      <el-scrollbar>
        <div
          v-for="(notice, index) in announcements"
          :key="index"
          class="notice-item"
        >
          <div class="notice-header">
            <h3>
              <el-icon><Bell /></el-icon>
              {{ notice.title }}
            </h3>
            <span class="notice-date">{{ notice.date }}</span>
          </div>
          <p v-html="formatContent(notice.content)"></p>
        </div>
      </el-scrollbar>
    </div>
    <!-- <div class="announcement-image">
      <img src="http://yd.sjcmc.cn:16578/wp-content/uploads/2025/03/leiyi.png" alt="" />
    </div> -->
  </div>
</template>

<script setup>
import { Bell } from "@element-plus/icons-vue";
import { ref, onMounted } from 'vue';
// import jsonAnnouncements from '@/data/announcements.json';// 格式化内容，将\n转换为<br>
const formatContent = (content) => {
  if (!content) return '';
  return content.replace(/\n/g, '<br>');
};

// 将JSON公告转换为与原始公告相同的格式
// 同时需要修改 convertJsonAnnouncements 函数，因为现在直接传入 jsonData
const convertJsonAnnouncements = (jsonData) => {
  return jsonData.announcements.map(jsonAnnounce => {
    const contentStr = jsonAnnounce.content.join('\n');
    const formattedDate = jsonAnnounce.date.replace(/\./g, '-');
    
    return {
      title: jsonAnnounce.title,
      content: contentStr,
      date: formattedDate
    };
  });
};
// 公告数据
const announcements = ref([
  {
    title: "游戏频道",
    content:
      "本服目前只有唯一官网渠道，交流群为：<a target='_blank' href='https://seerxin.com/url/group'>pd87126946</a>",
    date: "2025-01-16",
  }
]);

// 从CDN获取公告数据
const fetchAnnouncementsFromCDN = async () => {
  try {
    const response = await fetch('http://yd.sjcmc.cn:16578/announcements.json');
    if (!response.ok) {
      throw new Error('Network response was not ok');
    }
    const jsonData = await response.json();
    const convertedAnnouncements = convertJsonAnnouncements(jsonData);
    announcements.value = [...announcements.value, ...convertedAnnouncements];
    console.log(`已加载 ${announcements.value.length} 条公告`);
  } catch (error) {
    console.error('获取公告数据失败:', error);
  }
};


// 在组件挂载时追加公告数据
onMounted(() => {
  // fetchAnnouncementsFromCDN();
});
</script>

<style scoped lang="scss">
.announcement-panel {
  flex: 1;
  padding: 1.5rem;
  background: var(--el-bg-color-page);
  border-right: 1px solid var(--el-border-color-light);
  position: relative;
  display: flex;
  flex-direction: column;

  h2 {
    margin-bottom: 1.5rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--el-border-color-light);
    color: var(--el-text-color-primary);
    font-size: 1.4rem;
    font-weight: 600;
    padding-right: 0;
  }

  .announcement-content {
    flex: 1;
    overflow: hidden;
    height: calc(100% - 4rem);

    :deep(.el-scrollbar) {
      height: 100%;

      .el-scrollbar__wrap {
        height: 100%;
      }
    }

    .notice-item {
      margin-bottom: 1rem;
      padding: 1rem;
      border-radius: 8px;
      background: var(--el-bg-color);
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
      transition: all 0.3s ease;

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 16px 0 rgba(0, 0, 0, 0.1);
      }

      .pin-tag {
        margin-left: 8px;
        font-size: 12px;
        padding: 0 6px;
        height: 20px;
        line-height: 18px;
        border-radius: 4px;
        font-weight: normal;
        transform: translateY(-1px);
      }

      .notice-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 0.5rem;

        h3 {
          margin: 0;
          color: #2c3e50;
          font-size: 1.1rem;
          font-weight: 600;
          display: flex;
          align-items: center;
          gap: 0.5rem;

          .el-icon {
            color: #1890ff;
          }
        }
      }

      p {
        color: #666;
        line-height: 1.6;
        font-size: 0.95rem;
        margin: 0.5rem 0;
        white-space: pre-line;

        :deep(a) {
          color: var(--el-color-primary);
          text-decoration: none;
          transition: all 0.3s ease;
          
          &:hover {
            color: var(--el-color-primary-light-3);
            text-decoration: underline;
          }
        }
      }

      .notice-date {
        color: #999;
        font-size: 0.85rem;
      }
    }
  }

  .announcement-image {
    position: absolute;
    right: -1rem;
    top: -1rem;
    padding: 1rem;
    height: 100px;
    width: 140px;
    display: flex;
    align-items: flex-start;
    justify-content: flex-end;
    overflow: hidden;
    z-index: 1;
    pointer-events: auto;

    img {
      width: 100%;
      height: 100%;
      object-fit: contain;
      animation: float 3s ease-in-out infinite;

      &:hover {
        animation-play-state: paused;
      }
    }
  }
}
@keyframes float {
  0% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-10px) rotate(5deg);
  }
  100% {
    transform: translateY(0px) rotate(0deg);
  }
}
</style>